import { Injectable } from '@angular/core';
import { Http, Headers } from "@angular/http";
import { AppConfig } from "../config";
import { TurbStatus } from "../entity/turbstatus.model";
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/subject";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class TurbStatusService {
  private param = "turbStatuses";
  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }


  constructor(private http: InterceptorService) {

  }
  /**
   * 获取状态列表
   * 
   * @returns {Promise<TurbStatus[]>} 
   * @memberof TurbStatusService
   */
  getTurbStatuies(): Promise<TurbStatus[]> {
    return this.http.get(this.url)
    .toPromise()
    .then(response => {
      return response.json().code === '200' ? response.json().data as TurbStatus[]:[];
    })
    .catch(this.handleError);
  }
  /**
   * 获得风机状态
   * 
   * @returns {Promise<TurbStatus[]>} 
   * @memberof TurbStatusService
   */
  getTurbStatus(): Promise<TurbStatus[]> {
    return this.http.get(`${this.url}/turbine`)
      .toPromise()
      .then(response => {
        return response.json().code === '200' ? response.json().data as TurbStatus[]:[];
      })
      .catch(this.handleError);
  }
  /**
   * ��������״̬
   * 
   * @returns {Promise<TurbStatus[]>} 
   * @memberof TurbStatusService
   */
  getInverterStatus(): Promise<TurbStatus[]> {
    return this.http.get(`${this.url}/inverter`)
      .toPromise()
      .then(response => {
        return response.json().code === '200' ? response.json().data as TurbStatus[]:[];
      })
      .catch(this.handleError);
  }
  create(turbStatus: TurbStatus): Promise<TurbStatus> {
    return this.http
      .post(this.url, JSON.stringify(turbStatus))
      .toPromise()
      .then(res => res.json().code === '200' ?res.json().data:[])
      .catch(this.handleError);
  }
  update(turbStatus: TurbStatus): Promise<TurbStatus> {
    const url = `${this.url}`;
    return this.http
      .put(url, JSON.stringify(turbStatus))
      .toPromise()
      .then(() => turbStatus)
      .catch(this.handleError);
  }
  delete(id: number): Promise<void> {
    const url = `${this.url}/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }
  batchDelete(keyIds: number[]): Promise<boolean> {
    const apiUrl = `${this.url}?items=${keyIds}`;
    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response.json();
          switch (result.code) {
            case "200":
              return true;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }
  hasStatusName(statusVal: string, statusId: number, name: string): Observable<boolean> {
    const url = `${this.url}/${name}/${statusVal}/statusId/${statusId}`;
    return this.http.get(url).map(response => {
      let code = response.json().code;
      switch (code) {
        case "3001":
          return true;
        case "3002":
          return false;
      }

    }).catch(this.handleError);
  }
  hasStatusColor(statusColor: string, statusId: number): Observable<boolean> {
    statusColor = statusColor.replace(/\#/, "%23");
    const url = `${this.url}/statusColor/${statusColor}/statusId/${statusId}`;
    return this.http.get(url).map(response => {
      let code = response.json().code;
      switch (code) {
        case "3001":
          return true;
        case "3002":
          return false;
      }

    }).catch(this.handleError);
  }
  private handleError(error: any): Promise<any> {
    // console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }
}



// WEBPACK FOOTER //
// ./src/app/service/turbstatus.service.ts